package gorm1

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"go_orm/zap"
	"moul.io/zapgorm"
	"time"
)

func getDb() *gorm.DB {
	connArgs := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", "root", "fan1988", "127.0.0.1", 3306, "godbc")

	db, err := gorm.Open("mysql", connArgs)
	if err != nil {
		return nil
	}

	db.SingularTable(true) //如果使用gorm来帮忙创建表时，这里填写false的话gorm会给表添加s后缀，填写true则不会
	logger := zap.InitZap("D:/tmp/logs/zap/info.log", "D:/tmp/logs/zap/error.log", "info")
	db.SetLogger(zapgorm.New(logger))
	db.LogMode(true) //打印sql语句
	//开启连接池
	db.DB().SetMaxIdleConns(100)   //最大空闲连接
	db.DB().SetMaxOpenConns(10000) //最大连接数
	db.DB().SetConnMaxLifetime(30) //最大生存时间(s)
	return db
}

func createReturnId(db *gorm.DB) (int64, interface{}) {
	u := SysAccount{
		UserName:   "taotao",
		Password:   "532542523",
		CreateTime: time.Now(),
		UpdateTime: time.Now(),
	}

	effectedRows := db.Create(&u).RowsAffected

	return effectedRows, u.ID

}

func RunGorm() {
	createReturnId(getDb())

}
